<?php
// **************************************************************
//  Copyright 2007 AMC
//  Glenview, Illinois
//
//  File:      member_edit.php
//  Author:    Mickey Freeman (mfreeman@connect2amc.com)
//  Function:  Page for registering a member
//
//  Created: 08/16/07
//  Version: 1.0
//
//  Modified:
// **************************************************************
require_once 'init.php';

$msg = $_REQUEST[msg];

$obj_template      = new template();


// pulling the page task
$task             = $_REQUEST['task'];
$obj              = new ws_secure_user();
$obj->load($usr_id);


if (!$usr_id){
    $obj->usr_active = 1;
}
switch ($task) {
	case 'delete':
		
		$delete_id  = $_REQUEST['del_id']; 
		$obj_template->delete($delete_id);
	    $msg       = "template deleted successfully";
		header("Location: " .$_SERVER['PHP_SELF']. "?msg=".$msg);
		
		exit;
        break;
        
	case 'save': //saving a submitted registration form form
		$obj_template->load_from_request();
		//print_r($obj_template); 
		$errors = $obj_template->validate_template();
	

		if (count($errors) > 0){ // if there are errors then redisplay the form with the errors
		//  $title = $lang['Reg_title'];
		include ABSTRACT_PRIVATE_HEADER;

			
			// assigning variables to the template and displaying
			$page_data = array(
			                   'template'         => $obj_template->values,
			                   'errors'       => $errors,
			                   'cancel_link'  => $_SERVER['PHP_SELF'],
			                   'action_link'=> '?task=save',
		                       'projectlist'=> project::get_project_list() 
			                   );

			include 'templates/tpl_template_add.php';

			include ABSTRACT_PRIVATE_FOOTER;

		} else { // if there are no errors then register the member and log them in
			        
                                  
            $obj_template->save();
			   
			$msg = "New template added  successfully";  
			header("Location: " .$_SERVER['PHP_SELF']. "?msg=".$msg);
			exit();

		}
		break;

case 'add_template': // add url 
		include ABSTRACT_PRIVATE_HEADER;
		$page_data = array('action_link'=> '?task=save',
		                    //'projectlist'=> project::get_project_list()
		                     );
		include 'templates/tpl_template_add.php';
		include ABSTRACT_PRIVATE_FOOTER;
	
		break;
		
case 'edit': // edit url 
        $editId    = $_REQUEST['editto'];
        $page_data = array('template'   => $obj_template->get_template($editId) ,
                           'page_id'=>  $editId , 
                           'action_link'=> '?task=update',
                           'projectlist'=> project::get_project_list()   
		                  );

		include ABSTRACT_PRIVATE_HEADER;
		include 'templates/tpl_template_add.php';
		include ABSTRACT_PRIVATE_FOOTER;
		break;
case 'update':
	
	  $obj_template->load($page_id);
	  $obj_template->update_from_request();
	  $msg ="template detail updated successfully";
//	  echo '<pre>';print_r($obj_template);echo '</pre>';die();
	  $obj_template->save();
	  header("Location: " .$_SERVER['PHP_SELF']. "?msg=".$msg);
		exit();
	  break;
	
 default:
 //global $page_title, $sec, $user_access_level, $security;
 
 		$sql = "SELECT temp.*,project_name as pname,template_name as tname, 'Edit' as page_edit, 'Delete' as page_delete ". "FROM " . TEMPLATE_TABLE . " temp
 		LEFT join ".PROJECT_TABLE." as pr ON pr.project_id=temp.project_id and project_status=1  where template_status=1";
		     
		$obj_list = new app_list($sql, 'template_id', 'template_id', 'DESC', 'normal', 'normal', '0', '5', true, '25','','','','100%');
		
		$obj_list->sort_url = $_SERVER['PHP_SELF'];
		
		$obj_list->empty_message = '<span class="normal"> There are currently no template in the system.</span>';
		$obj_list->add_column('template_id', 'Id ', '', '', '', '', true);
		$obj_list->add_column('tname', 'Template Name', '', '', '%%%pname%%%', '', true,'','text','','');
		//$obj_list->add_column('pname', 'Project Name', '', '', '#', '', true);
		$obj_list->add_column('page_edit', 'Edit', '', '', $_SERVER['PHP_SELF'] . '?task=edit&editto=%%%id%%%', '', false);
		$obj_list->add_column('page_delete', 'Delete', '', '', '#', 'return askFirstBeforeDelete("%%%id%%%")', false);
		
		$obj_list->init_list($_SERVER['PHP_SELF']);
		
		include ABSTRACT_PRIVATE_HEADER;
		//include 'abstract_subnav.inc';
		echo '<div style="text-align:right;width:95%;">
          <input type="button" value="Add New Template" onclick="location.href=\'?task=add_template\'"/>
        </div><br>';
		if($msg!='')
		{
		echo '<div class="gray_box">
		<em>'.$msg.'</em></div>';
		}
		echo '<div class="gray_box">';		
		echo '<strong><span style="text-align:left;">Templates:</span> </strong><br/>';
		echo '<div style="text-align:left">';		
		//echo "<span style='color: #ff0000'>$msg</span><br />";
		echo $obj_list->get_html();
		echo '</div> </div>';

		include ABSTRACT_PRIVATE_FOOTER;
		break;
		
}
?>
<script>
  function askFirstBeforeDelete(id)
  {
  	if(window.confirm("Are you sure you want to remove this template")){
  		document.location.href = "?task=delete&del_id="+id;
  		return true;
  	}
  	return false;  	
  }
  function popwin5(url) {
   win = window.open(url,"","menubar=no,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=no");
   // This probably only works in firefox
   //win.onblur = function() {win.close();}
   // Dan didt like this effect for the reports anyway
}
</script>